<template>
  <!-- 卡片 -->
  <el-card class="box-card el-card-flex">
    <div slot="header" class="clearfix">
      <span>
        <i class="el-icon-warning-outline"></i>
        我创建的
      </span>
    </div>
    <!-- 表格 -->
    <div class="text item">
      <el-form :inline="true" class="demo-form-inline" style="float: left;height:43px" @submit.native.prevent>
        <el-form-item>
          <el-input clearable placeholder="请输入任务编码/内容" v-model="textSearch" @keyup.enter.native="searchTabel"
            @clear="searchTabel" style="width:240px;">
            <el-button slot="append" icon="el-icon-search" @click="searchTabel"></el-button>
          </el-input>
          <el-button type="primary" style="margin-left: 10px;" @click="agentSearchDialog">
            高级查询
            <i class="el-icon-magic-stick"></i>
          </el-button>
        </el-form-item>
        <el-form-item style="float:right">
          <el-button type="primary" @click='handleTaskAdd()' plain>
            创建任务
            <i class="el-icon-plus"></i>
          </el-button>
        </el-form-item>
      </el-form>
      <div class="el-flex el-box-column">
        <el-table border height="100%" style="border-bottom: 40px solid transparent;" :data="tableData" stripe ref="firstTale">
          <el-table-column type="index" min-width="55px" label="序号" align="center" sortable show-overflow-tooltip></el-table-column>
          <el-table-column prop="taskCode" min-width="120px" label="任务编码" align="center" sortable show-overflow-tooltip></el-table-column>
          <el-table-column prop="taskContent" min-width="150px" label="任务内容" align="left" sortable
            show-overflow-tooltip></el-table-column>
          <el-table-column prop="taskStatusName" min-width="100px" label="任务状态" align="center" sortable
            show-overflow-tooltip></el-table-column>
          <el-table-column prop="principal" min-width="100px" label="负责人" align="center" sortable show-overflow-tooltip></el-table-column>
          <el-table-column prop="issueDate" min-width="100px" label="发布日期" align="center" sortable
            show-overflow-tooltip></el-table-column>
          <el-table-column prop="expirationDate" min-width="150px" label="要求完成日期" align="center" sortable
            show-overflow-tooltip></el-table-column>
          <el-table-column prop="control" align="center" label="操作" fixed="right" width='190px'>
            <template slot-scope="scope">
              <el-button class="td-btn-icon" type="text" title="查看" @click='handleDetails(scope.$index, scope.row)'
                icon="el-icon-zoom-in"></el-button>
              <el-button class="td-btn-icon" type="text" title="编辑" :disabled='scope.row.taskStatusName !="起草"' @click='handleTaskEdit(scope.$index, scope.row)'
                icon="el-icon-edit"></el-button>
              <el-button class="td-btn-icon" type="text" title="发布" :disabled='scope.row.taskStatusName !="起草"' @click='handleTaskRelease(scope.$index, scope.row)'
                icon="el-icon-position"></el-button>
              <el-button class="td-btn-icon" type="text" title="删除" :disabled='scope.row.taskStatusName !="起草"'
                @click.native.prevent="deleteRow(scope.$index, scope.row)" icon="el-icon-delete"></el-button>
            </template>
          </el-table-column>

        </el-table>
        <el-pagination style="background: #fff; height: 32px; padding-top: 11px;left: 7px;" class="pagination"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" background :current-page="currentPage"
          :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
      </div>
    </div>
    <el-drawer title="高级查询" :before-close="handleAgentSearchClose" :visible.sync="dialogAgentSearchVisible" direction="rtl"
      custom-class="demo-drawer" ref="drawer" :close-on-press-escape="false">
      <div class="demo-drawer__content el-box-column" style="height: 100%; margin-left: 18px;">
        <el-form label-width="100px" :model="formInline" ref="formInline" class="el-flex">
          <el-form-item label="任务编码:" prop="taskCode">
            <el-col :span="18">
              <el-input placeholder="请输入编码" v-model="formInline.taskCode"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="任务内容:">
            <el-col :span="18">
              <el-input placeholder="请输入内容" v-model="formInline.taskContent"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="状态:">
            <el-col :span="16">
              <el-select placeholder="状态" v-model="formInline.taskStatus">
                <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="负责人:">
            <el-col :span="12">
              <el-input placeholder="请输入负责人" v-model="formInline.principal"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="发布日期:">
            <el-col :span="23">
              <el-date-picker :unlink-panels='true' style="width: 100%" v-model="startDate" type="daterange"
                range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" placeholder="选择发布日期" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="要求完成日期:">
            <el-col :span="23">
              <el-date-picker :unlink-panels='true' style="width: 100%" v-model="endDate" type="daterange"
                range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" placeholder="选择要求完成日期" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer" style="text-align: right; margin-bottom: 10px; padding-right:10px;">
          <el-button type="primary" @click="handleSearchSure">
            确定
            <i class="el-icon-document-checked el-icon"></i>
          </el-button>
          <el-button @click="handleSearchClose">
            重置
            <i class="el-icon-refresh-left"></i>
          </el-button>
        </div>
      </div>
    </el-drawer>
  </el-card>
</template>
<script>
  import {
    Tree,
    Dialog,
    Table,
    TableColumn,
    Pagination,
    MessageBox,
    Message,
    Tag,
    Upload,
    Drawer
  } from 'element-ui';
  import {
    taskList, //列表数据
    taskDel, //删除任务
    comboBoxData, //状态
    taskStatus, //发布
  } from '@/api/missionCenter/missionCenter.js'
  export default {
    name: 'myCreate.vue',
    components: {
      'el-tree': Tree, //树形菜单
      'el-dialog': Dialog, //对话框
      'el-table': Table, //表格
      'el-table-column': TableColumn, //表格列
      'el-pagination': Pagination, //分页
      'el-tag': Tag, //标签
      'el-upload': Upload,
      'el-drawer': Drawer
    },
    data() {
      let domainInlineSearchVal = {
        category: "1",
        expirationDateStart: '',
        expirationDateEnd: '',
        expression: "",
        principal: "",
        qutryType: "1",
        taskCode: "",
        taskContent: "",
        taskStatus: "",
        issueDateStart: '',
        issueDateEnd: ''
      }
      return {
        startDate: '',
        endDate: '',
        formInline: JSON.parse(JSON.stringify(domainInlineSearchVal)), // 系统管理高级查询条件
        dialogAgentSearchVisible: false,
        textSearch: "",
        pageSize: 20,
        total: 0,
        currentPage: 1,
        tableData: [],
        isTreeTable: true,
        options: []
      };
    },

    created() {
      this.getTabTree(this.formInline)
      window.onresize = () => {
        this.$refs.firstTale.doLayout()
      }
    },
    methods: {
      getTabTree(formInline) {
        taskList(this.currentPage, this.pageSize, formInline).then(res => {
          if (res.data.code == 10000) {
            this.tableData = res.data.data.records
            this.total = res.data.data.total
            setTimeout(() => {
              this.$refs.firstTale.doLayout() //重绘表格宽度，解决表头对不齐的问题
            }, 0);
          }
        }).catch(res => {
          resolve([])
        })
      },
      deleteRow(index, row) { //删除行数据
        var id = row.taskId
        MessageBox.confirm('您是否确定删除选中内容', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            taskDel(id).then(res => {
              if (res.data.code == 10000) {
                Message({
                  type: 'success',
                  message: '删除成功'
                });
                this.getTabTree(this.formInline)
              }

            }).catch(res => {
              Message({
                type: 'warning',
                message: "删除失败"
              });
            })
          })
      },
      handleTaskEdit(index, row) {
        row.justLook = 'N'
        this.giveFuncEvt(row.taskId)
      },
      handleTaskAdd() {
        this.giveFuncEvt('object')
      },
      /*跳转到新增功能路由组件*/
      giveFuncEvt(row) {
        //this.multiRouters是全局提供的自动添加多级路由的方法
        this.$router.addRoutes(this.multiRouters([{
          path: 'createTask',
          name: 'createTask', //自定义name名称
          component: () => import('@/views/thematicPortal/missionCenter/createTask'),
          meta: {
            title: '创建任务'
          }
        }]));
        this.$router.push(this.routerMatch({
          name: "createTask", //顶部导航+侧边导航
          params: {
            row: row
          }
        }))
      },
      /*跳转到新增功能路由组件*/
      handleDetails(index, row) {
        row.justLook = "Y"
        sessionStorage.taskId = row.taskId;
        sessionStorage.showTab = '我创建的';
        row.showTab = '我创建的'
        this.$router.addRoutes(this.multiRouters([{
          path: 'handleDetails',
          name: 'handleDetails', //自定义name名称
          component: () => import('@/views/thematicPortal/missionCenter/handleDetails'),
          meta: {
            title: '办理'
          }
        }]));
        this.$router.push(this.routerMatch({
          name: "handleDetails", //顶部导航+侧边导航
          params: {
            row: row
          }
        }))
      },
      //发布按钮
      handleTaskRelease(index, row) {
        taskStatus(row.taskId, 'c2cd78d1eea54923970dfdcf5898ce92').then((res) => {
          if (res.data.code == 10000) {
            Message({
              type: 'success',
              message: "发布成功"
            });
            this.getTabTree(this.formInline)
          }
        })
      },
      // 搜索事件
      searchTabel() {
        if (this.textSearch) { // 有数据,是要搜索
          this.currentPage = 1
          this.formInline.expression = this.textSearch
          this.getTabTree(this.formInline)
        } else { // 搜索输入框是空的,说明是要重置,不搜索了,展示正常的全部数据
          this.searchData = ''
          this.formInline.expression = ''
          this.getTabTree(this.formInline)
        }
      },
      // 高级查询弹框显示
      agentSearchDialog() {
        this.textSearch = ''
        comboBoxData('8496f6fed2194b7fb5c14d0667aeb592').then((res) => {
          if (res.data.code == 10000) {
            this.options = res.data.data
          }
        })
        this.dialogAgentSearchVisible = true
      },
      // 高级查询弹框关闭事件
      handleAgentSearchClose() {
        this.dialogAgentSearchVisible = false;
      },
      handleSearchSure() {
        this.currentPage = 1
        this.formInline.issueDateStart = this.startDate[0]
        this.formInline.issueDateEnd = this.startDate[1]
        this.formInline.expirationDateStart = this.endDate[0]
        this.formInline.expirationDateEnd = this.endDate[1]
        this.formInline.qutryType = '2'
        this.getTabTree(this.formInline)
        this.dialogAgentSearchVisible = false;
        setTimeout(()=>{
          this.formInline.qutryType = '1'
        })

      },
      // 高级查询点击弹框取消事件
      handleSearchClose() {
        this.dialogAgentSearchVisible = false;
        this.startDate = ''
        this.endDate = ''
        this.formInline.issueDateStart = ''
        this.formInline.issueDateEnd = ''
        this.formInline.taskCode = ''
        this.formInline.taskContent = ''
        this.formInline.taskStatus = ''
        this.formInline.principal = ''
        this.currentPage = 1
        this.getTabTree(this.formInline)
      },
      handleSizeChange(val) { //每页条数
      setTimeout(()=>{
        this.pageSize = val;
        this.getTabTree(this.formInline)
      },0)
      },
      handleCurrentChange(val) { //当前页数
        this.currentPage = val;
        this.getTabTree(this.formInline)
      },

    }
  };
</script>

<style lang="scss">
  .el-card__body {
    position: relative;
  }

  .pagination.el-pagination {
    position: absolute;
    bottom: 5px;
    left: 15px;
  }

  .el-form-tag {
    margin-bottom: 8px !important;
  }

  .el-form-tag .el-tag {
    margin-bottom: 10px;
    margin-left: 0 !important;
    margin-right: 10px !important;
  }

  .el-input:not(.el-input--suffix) .el-input__inner[readonly] {
    background: none !important;
  }
</style>
